package com.itheima.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

//实现类 从数据库查询数据
@DubboService
public class UserApiImpl  implements UserApi{

    @Autowired
    private UserMapper userMapper;

    //根据手机号码查询用户
    public User findByMobile(String mobile) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("mobile",mobile);
        return userMapper.selectOne(qw);
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    //查询所有用户信息
    @Override
    public List<User> findall() {
        return userMapper.selectList(Wrappers.emptyWrapper());
    }

    //更新
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    //通过id查询
    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }


    //更具环信查询用户信息
    @Override
    public User findByHuanxin(String huanxinId) {
        //QueryWrapper查询包装器
        QueryWrapper<User> qw = new QueryWrapper<>();
        //去数据库中 从用户表 查找到 hx_user 根据前端传过来的环信id 查询用户信息
        qw.eq("hx_user",huanxinId);
        //返还前端查询一条数据的结果
        return userMapper.selectOne(qw);
    }

    //更新手机号
    @Override
    public void updatePhoneById(Long userId, String phone) {
        User user=new User();
        user.setMobile(phone);
        user.setId(userId);
        userMapper.updateById(user);
    }
}
